package com.treasure.web.listener;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.treasure.infrastructure.model.event.ConversationEvent;
import com.treasure.infrastructure.model.po.AiRobot;
import com.treasure.infrastructure.model.po.ConversationDetails;
import com.treasure.infrastructure.repository.dao.mapper.AiRobotMapper;
import com.treasure.infrastructure.repository.dao.mapper.ConversationDetailsMapper;
import com.treasure.web.service.IAiRobotService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;


/**
 * @author sunhongmin
 * @date 2024/1/14 10:22
 * @description
 */
@Component
@RequiredArgsConstructor
public class ConversationEventListener {

    private final ConversationDetailsMapper conversationDetailsMapper;
    private final IAiRobotService aiRobotService;

    @EventListener
    @Async
    public void onApplicationEvent(@NotNull ConversationEvent conversationEvent) {
        ConversationDetails conversationDetails = new ConversationDetails();
        conversationDetails.setCorpId(conversationEvent.getCorpId());
        conversationDetails.setUserId(conversationEvent.getUserId());
        conversationDetails.setQuestion(conversationEvent.getQuestion());
        conversationDetails.setAnswer(conversationEvent.getAnswer());
        conversationDetails.setCreateTime(conversationEvent.getCreateTime());
        conversationDetails.setRobotId(aiRobotService.getCorpDefaultRobotId(conversationEvent.getCorpId()));
        conversationDetails.setInputTokens(conversationEvent.getInputTokens());
        conversationDetails.setOutputTokens(conversationEvent.getOutputTokens());
        conversationDetailsMapper.insert(conversationDetails);
    }
}
